<?PHP

class alps_class
{
	
	function __construct()
	{
		$this->y = 1;
		$this->x = 1;
	}
	
	function deduct_ticket()
	{
		global $db;
		$user_id = USER_ID;
		$sql = "SELECT * FROM `user_items` WHERE `type`='ticket' AND `section` = 'transport' AND `item`='alps' AND `quantity`>'0' AND `assoc_id`='$user_id'";
		$ticket_info = $db->return_first_result($sql);
		
		$error = 0;
		$error_message = '';
		
		//display_var($ticket_info);
		
		
		if ($ticket_info['quantity'] < 1 && $error == 0)
		{
			$error++;
			$error_message .= 'You do not have a ticket to get you there.';
		}
		
		
		if ($error < 1)
		{
		
			//$alps->display_nav();
			//$alps->display_options();
			echo 'Welcome to the Alps.';
		}
		else 
		{
			echo $error_message;
		}
		
		return true;
	}
	
	function get_there()
	{
		global $db;
		
		$db->query("INSERT INTO `user_alps` ( `assoc_id` , `arrive_time` ) VALUES ('".USER_ID."', '".NOW."')");
		
		return true;
	}
	
	function get_square($map_info, $x, $y)
	{
		
		global $db;
		
		$default_img = 'http://images.europa100ad.com/3big.png';
		
		$spot = @$map_info["$x.$y"]['img'];
		if (strlen($spot) < 1)
		{
			$db->query("INSERT INTO `core_alps` (`x`, `y`, `coord`, `img`, `water_n`, `water_e`, `water_s`, `water_w`) VALUES ('$x', '$y', '$x.$y', '$default_img', 'no', 'no', 'no', 'no')");
			log_memory("Alps -> Added land block $x.$y");
			$spot = $default_img;
		}
		
		return $spot;
	}
	
	function display_nav()
	{
		global $db;
		
		$new_y = $this->y + 1;
		$new_x = $this->x;
		$up_link = ahref("/alps/nav/$new_x/$new_y/",'Up');
		
		$new_y = $this->y - 1;
		$new_x = $this->x;
		if ($new_x > 0 && $new_y > 0)
		{
			$down_link = ahref("/alps/nav/$new_x/$new_y/",'Down');
		}
		else 
		{
			$down_link = 'Down';
		}
		
		$new_y = $this->y;
		$new_x = $this->x - 1;
		if ($new_x > 0 && $new_y > 0)
		{
			$left_link = ahref("/alps/nav/$new_x/$new_y/",'&lt;');
		}
		else 
		{
			$left_link = '&lt;';
		}
		
		$new_y = $this->y;
		$new_x = $this->x + 1;
		$right_link = ahref("/alps/nav/$new_x/$new_y/",'&gt;');
		
		$x = $this->x;
		$x_neg = $x - 2;
		$x_pos = $x + 2;
		
		$y = $this->y;
		$y_neg = $y - 2;
		$y_pos = $y + 2;
		
		$sql = "SELECT * FROM `core_alps` WHERE `x`>'$x_neg' AND `x`<'$x_pos' AND `y`>'$y_neg' AND `y`<'$y_pos'";
		$map_info = $db->fetch_real_array($sql,'coord');
		
		//display_var($map_info);
		
		
		// TOP
		$top_l_x = $x - 1;
		$top_l_y = $y + 1;
		$top_l = $this->get_square($map_info, $top_l_x,$top_l_y);
		
		$top_m_x = $x;
		$top_m_y = $y + 1;
		$top_m = $this->get_square($map_info, $top_m_x,$top_m_y);
		
		$top_r_x = $x + 1;
		$top_r_y = $y + 1;
		$top_r = $this->get_square($map_info, $top_r_x,$top_r_y);
		
		// MID
		$mid_l_x = $x - 1;
		$mid_l_y = $y;
		$mid_l = $this->get_square($map_info, $mid_l_x,$mid_l_y);
		
		$mid_m_x = $x;
		$mid_m_y = $y;
		$mid_m = $this->get_square($map_info, $mid_m_x,$mid_m_y);
		
		$mid_r_x = $x + 1;
		$mid_r_y = $y;
		$mid_r = $this->get_square($map_info, $mid_r_x,$mid_r_y);
		
		
		// BUM
		$bum_l_x = $x - 1;
		$bum_l_y = $y - 1;
		$bum_l = $this->get_square($map_info, $bum_l_x,$bum_l_y);
		
		$bum_m_x = $x;
		$bum_m_y = $y - 1;
		$bum_m = $this->get_square($map_info, $bum_m_x, $bum_m_y);
		
		$bum_r_x = $x + 1;
		$bum_r_y = $y - 1;
		$bum_r = $this->get_square($map_info, $bum_r_x, $bum_r_y);
		
		
		$output = '<table style="width: 96px; height: 96px; text-align: left;" border="0"
					 cellpadding="0" cellspacing="0">
					  <tbody>
					    <tr>
					      <td></td>
					      <td style="text-align: center;">'.$up_link.'</td>
					      <td></td>
					    </tr>
					    <tr>
					      <td>'.$left_link.'</td>
					      <td>
					      <table style="text-align: left; height: 96px; width: 96px;"
					 border="0" cellpadding="0" cellspacing="0">
					        <tbody>
					          <tr>
					            <td><img src="'.$top_l.'"></td>
					            <td><img src="'.$top_m.'"></td>
					            <td><img src="'.$top_r.'"></td>
					          </tr>
					          <tr>
					            <td><img src="'.$mid_l.'"></td>
					            <td><img src="'.$mid_m.'"></td>
					            <td><img src="'.$mid_r.'"></td>
					          </tr>
					          <tr>
					            <td><img src="'.$bum_l.'"></td>
					            <td><img src="'.$bum_m.'"></td>
					            <td><img src="'.$bum_r.'"></td>
					          </tr>
					        </tbody>
					      </table>
					      </td>
					      <td>'.$right_link.'</td>
					    </tr>
					    <tr>
					      <td></td>
					      <td style="text-align: center;">'.$down_link.'</td>
					      <td></td>
					    </tr>
					  </tbody>
					</table>';
		echo $output;
		return true;
	}
	
	function display_options()
	{
		return true;
	}
	
	function check_if_first_visit()
	{
		return true;
	}
	
	
	function build_river($start_x,$start_y,$last_plot_exit)
	{
		global $max_build,$db;
		
		$max_build++;
		
		log_memory("Building a new river block.\n\n Current build platz: $max_build \n\n x=$start_x,y=$start_y");
		
		if ($max_build > 20)
		{
			echo '<pre>max river blocks built</pre>';
			exit;
			
		}
		
		$plot = array();
		
			$plot['x'] = $start_x;
			$plot['y'] = $start_y;
			
			$insert_e = 'no';
			$insert_n = 'no';
			$insert_s = 'no';
			$insert_w = 'no';
			
			$rand = mt_rand(1,4);
			
			switch ($last_plot_exit) {
				case 'north':
					$field_last = 'water_s';
					$insert_s = 'yes';
					$next = array('north','east','west');
					break;
		
				case 'east':
					$field_last = 'water_w';
					$insert_w = 'yes';
					$next = array('north','south','west');
					break;
					
				case 'south':
					$field_last = 'water_n';
					$insert_n = 'yes';
					$next = array('south','east','west');
					break;
					
				case 'west':
					$field_last = 'water_e';
					$insert_e = 'yes';
					$next = array('north','east','south');
					break;
			
				default:
					echo 'error'; 
					break;
			}		
				
			$next_spot = array_rand($next);
			
			switch ($next_spot) {
				case 'north':
					$exit = 'north';
					$field = 'water_n';
					$insert_n = 'yes';
					$next_plot['x'] = $plot['x'];
					$next_plot['y'] = $plot['y'] + 1;
					break;
		
				case 'east':
					$exit = 'east';
					$field = 'water_e';
					$insert_e = 'yes';
					$next_plot['x'] = $plot['x'] + 1;
					$next_plot['y'] = $plot['y'];
					break;
					
				case 'south':
					$exit = 'south';
					$field = 'water_s';
					$insert_s = 'yes';
					$next_plot['x'] = $plot['x'];
					$next_plot['y'] = $plot['y'] - 1;
					break;
					
				case 'west':
					$exit = 'west';
					$field = 'water_w';
					$insert_w = 'yes';
					$next_plot['x'] = $plot['x'];
					$next_plot['y'] = $plot['y'] - 1;
					break;
			
				default:
					echo 'error'; 
					break;
			}
			
			if ($insert_n == 'yes' && $insert_e == 'yes')
			{
				$img = URL_IMAGES . 'water_ne.png';
			}
			elseif ($insert_n == 'yes' && $insert_s == 'yes')
			{
				$img = URL_IMAGES . 'water_ns.png';
			}
			elseif ($insert_n == 'yes' && $insert_w == 'yes')
			{
				$img = URL_IMAGES . 'water_nw.png';
			}
			elseif ($insert_e == 'yes' && $insert_s == 'yes')
			{
				$img = URL_IMAGES . 'water_es.png';
			}
			elseif ($insert_e == 'yes' && $insert_w == 'yes')
			{
				$img = URL_IMAGES . 'water_ew.png';
			}
			elseif ($insert_s == 'yes' && $insert_w == 'yes')
			{
				$img = URL_IMAGES . 'water_sw.png';
			}
			else 
			{
				$img = URL_IMAGES . '3big.png';
			}
			
			
			$sql = "UPDATE `core_alps` SET `$field`='yes',`$field_last`='yes',`img`='$img' WHERE `x`='{$plot['x']}' AND `y`='{$plot['y']}'";
			$db->query($sql);
			if ($db->affected_rows < 1)
			{
				$db->query("INSERT INTO `core_alps` ( `x` , `y` , `coord` , `img` , `water_n` , `water_e` , `water_s` , `water_w` ) VALUES ('{$plot['x']}', '{$plot['y']}', '{$plot['x']}.{$plot['y']}', '$img', '$insert_n', '$insert_e', '$insert_s', '$insert_w')");
			}
			
			$this->build_river($next_plot['x'],$next_plot['y'],$exit);
		
	}
	
}

?>